package cn.zsc.netdisk.controller;

import cn.zsc.netdisk.pojo.Advise;
import cn.zsc.netdisk.response.Code;
import cn.zsc.netdisk.response.Result;
import cn.zsc.netdisk.service.AdviseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author ccying
 * @date 2021/3/7
 */
@RestController
@Slf4j
@RequestMapping("/advise")
public class AdviseController {
    private final AdviseService service;

    public AdviseController(AdviseService service) {
        this.service = service;
    }

    /**
     * 请求获取用户通知
     * @param offset 分页起始坐标
     * @param row 查询条数
     * */
    @CrossOrigin
    @GetMapping("/get/{offset}/{row}")
    public Result getAdvise(@PathVariable int offset, @PathVariable int row) {
        System.out.println("*************************************************");
        log.info("请求获取用户通知");
        Advise[] advises = this.service.get(offset, row);
        return Result.success(advises);
    }

    /**
     * 请求一键已读通知
     * */
    @CrossOrigin
    @GetMapping("/isRead")
    public Result isWriteAll() {
        System.out.println("*************************************************");
        log.info("请求一键已读通知");
        try {
            this.service.isReadAll();
            return Result.success();
        } catch (Exception e) {
            log.error(e.toString());
            return Result.fail(Code.ERROR);
        }
    }

    /**
     * 请求读通知
     * @param id 通知ID
     * */
    @CrossOrigin
    @GetMapping("/isRead/{id}")
    public Result isWrite(@PathVariable int id) {
        System.out.println("*************************************************");
        log.info("请求读通知");
        try {
            this.service.isRead(id);
            return Result.success();
        } catch (Exception e) {
            log.error(e.toString());
            return Result.fail(Code.ERROR);
        }
    }

    /**
     * 请求一键清除通知
     * */
    @CrossOrigin
    @GetMapping("/delete")
    public Result deleteAll() {
        System.out.println("*************************************************");
        log.info("请求一键清除通知");
        try {
            this.service.deleteAll();
            return Result.success();
        } catch (Exception e) {
            log.error(e.toString());
            return Result.fail(Code.ERROR);
        }
    }
}
